<template>
  <div>
    <el-upload
      class="upload-demo"
      :action="uploadImgUrl"
      name="file"
      :headers="getHeaders"
      :data="extParams"
      :show-file-list="false"
      :before-upload="handleBeforeUpload"
      :on-success="handleUploadSuccess"
      :on-error="handleUploadError"
    >
      <el-button size="small" type="primary">点击上传</el-button>
      <template #tip>
        <div class="el-upload__tip">
          只能上传{{ fileTypeList.join("-") }}文件
        </div>
      </template>
    </el-upload>
  </div>
</template>

<script>
import { getToken,getCookie } from "@/utils/auth";

export default {
  props: {
    fileTypeList: {
      type: Array,
      default: () => ["image/jpeg", "image/jpg", "image/png", "image/gif"],
    },
    extParams: {
      type: Object,
      default: () => ({}),
    },
    uploadImgUrl: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      fileList: [],
    };
  },
  computed: {
    getHeaders() {
      return {
        Authorization: "Bearer " + getToken(),
        Authorization1: getCookie("member_id"),

      };
    },
  },
  methods: {
    handleBeforeUpload(file) {
      // 验证类型
      const fileType = file.type;
      if (this.fileTypeList.indexOf(fileType) == -1) {
        this.$message.error("请上传正确的文件");
        return false;
      }
      this.loading = this.$loading({
        lock: true,
        text: "上传中",
        background: "rgba(0, 0, 0, 0.7)",
      });
    },
    handleUploadError() {
      this.$message({
        type: "error",
        message: "上传失败",
      });
      this.loading.close();
    },
    handleUploadSuccess(res) {
      // console.log("res", res);
      this.$emit("uploadSuccess", res);
      this.loading.close();
    },
  },
};
</script>

<style scoped></style>
